from counterpoint.exceptions import Http403
from counterpoint.helpers import render_response
from django.conf import settings
from django.contrib.auth import REDIRECT_FIELD_NAME
from django.http import HttpResponseRedirect
from django.utils.http import urlquote


def forum_staff_required(view_func):
    """
    Ensures that the user visiting the page is a
    forum staff member.
    """

    def _checkstaff(request, *args, **kwargs):
        if not request.user.is_authenticated():
            redir_url = '%s?%s=%s' % (settings.LOGIN_URL,
                                      REDIRECT_FIELD_NAME,
                                      urlquote(request.get_full_path()))
            return HttpResponseRedirect(redir_url)
        if request.forum_user.access == 'S':
            return view_func(request, *args, **kwargs)
        raise Http403
    return _checkstaff


def forum_moderator_required(view_func):
    """
    Ensures that the user visiting the page is a
    forum staff member or moderator.
    """

    def _checkmoderator(request, *args, **kwargs):
        if not request.user.is_authenticated():
            redir_url = '%s?%s=%s' % (settings.LOGIN_URL,
                                      REDIRECT_FIELD_NAME,
                                      urlquote(request.get_full_path()))
            return HttpResponseRedirect(redir_url)
        if request.forum_user.access in ['S', 'M']:
            return view_func(request, *args, **kwargs)
        raise Http403
    return _checkmoderator
